const axios = require("axios");
const fs = require("fs");
const cheerio  = require("cheerio");

async function loadSuning( keyword ) {
      let options = {
            url: "https://search.suning.com/%E5%B0%8F%E7%B1%B3/",
            headers: {
                  "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
            }   
      }
      let response = await axios(options);
      
      let $ = cheerio.load( response.data );

      let price = $(".price-box span");
      let img   = $(".img-block a");
      let title = $(".title-selling-point a");

      let arr = [];
      for(var i = 0 ; i < price.length ; i ++){
            arr.push( {
                 price : price.eq(i).text(),
                 src : img.eq(i).attr("data-lazy-img"),
                 title : title.eq(i).text()
            });
      }

      fs.writeFile("./suning.json" , JSON.stringify(arr) , err => {
            if( err ){
                  throw err; 
            }
            console.log( "小米数据爬取成功" );
      })
}

loadSuning("小米");